package com.lingkang.dbtodoc.controller;

import cn.hutool.core.lang.Assert;
import com.lingkang.dbtodoc.constants.HttpCode;
import com.lingkang.dbtodoc.dto.ResponseResult;
import com.lingkang.dbtodoc.entity.DataBaseSchema;
import com.lingkang.dbtodoc.entity.TableSchema;
import com.lingkang.dbtodoc.pojo.DbConnect;
import com.lingkang.dbtodoc.service.DBService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 绫小路
 * @date 2021/2/1 0:29
 * @description
 */
@Controller
@RequestMapping("/main")
public class MainController {

  @Autowired
  private HttpServletRequest request;
  @Autowired
  private DBService dbService;

  @Autowired
  private JdbcTemplate jdbcTemplate;

  @GetMapping("test")
  @ResponseBody
  public ResponseResult test() {
    dbService.getDataBaseSchema();
    return new ResponseResult();
  }

  /**
   * data: JSON.stringify(data.field), contentType: 'application/json;charset=UTF-8',
   */
  @PostMapping("conncet")
  @ResponseBody
  public ResponseResult conncet(@RequestBody DbConnect connect) {
    Assert.notBlank(connect.getDriverClassName(), "数据库类型不能为空！");
    Assert.notBlank(connect.getUrl(), "URL不能为空！");
    Assert.notBlank(connect.getUsername(), "账号不能为空！");
    Assert.notBlank(connect.getPassword(), "密码不能为空！");
    String result = dbService.updateConnect(connect, request);
    if (result == null) {
      request.getSession().setAttribute("connect", connect);
      return new ResponseResult(HttpCode.OK, "连接成功！");
    }
    return new ResponseResult(HttpCode.FAIL, result);
  }

  @GetMapping("dataBaseSchema")
  @ResponseBody
  public ResponseResult dataBaseSchema() {
    List<DataBaseSchema> dataBaseSchema = dbService.getDataBaseSchema();
    return new ResponseResult(HttpCode.OK, dataBaseSchema);
  }

  //修改表注释
  @PostMapping("updateTableComment")
  @ResponseBody
  public ResponseResult updateTableComment(String table, String comment) {
    Assert.notBlank(table, "表名不能为空！");
    Assert.notBlank(comment, "描述不能为空！");
    dbService.updateTableComment(table, comment);
    return new ResponseResult(HttpCode.OK, "更新成功！");
  }

  @GetMapping("exportPDF")
  public String exportPDF(String type) {
    List<DataBaseSchema> dataBaseSchema = dbService.getDataBaseSchema();
    request.setAttribute("data", dataBaseSchema);
    if ("detail".equals(type)) {
      List<TableSchema> tableSchema = dbService.getTableSchema();
      request.setAttribute("table", tableSchema);
      return "pdf-table";
    } else {
      return "pdf";
    }
  }

  @GetMapping("tableDetails")
  public String tableDetails(String tableName) {
    Assert.notBlank(tableName, "表名不能为空！");
    request.setAttribute("tableName", tableName);
    return "details-table";
  }

  @GetMapping("getTableSchema")
  @ResponseBody
  public ResponseResult getTableSchema(String tableName) {
    Assert.notBlank(tableName, "表名不能为空！");
    TableSchema tableSchema = dbService.getTableSchema(tableName);
    return new ResponseResult(HttpCode.OK, tableSchema);
  }

  //修改字段注释
  @PostMapping("updateColumnComment")
  @ResponseBody
  public ResponseResult updateColumnComment(String tableName, String columnName, String comment) {
    Assert.notBlank(columnName, "字段不能为空！");
    Assert.notBlank(tableName, "表名不能为空！");
    dbService.updateColumnComment(tableName, columnName, comment);
    return new ResponseResult(HttpCode.OK, "修改成功！");
  }


}
